package web;

import java.io.IOException;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import beans.Member;
import beans.TypeMessage;
import beans.Typer;
import service.TypeMessageService;
import service.TypeService;
import serviceimpl.TypeMessageServiceIpml;
import serviceimpl.TypeServiceIpmt;

@WebServlet("/matchServlet")
public class matchServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String p1 = request.getParameter("questions[1]");
		String p2 = request.getParameter("questions[2]");
		if (p1.equals("A") && p2.equals("A")) {
			request.getSession().setAttribute("answer", "1");
		} else if (p1.equals("A") && p2.equals("B")) {
			request.getSession().setAttribute("answer", "2");
		} else if (p1.equals("B") && p2.equals("B")) {
			request.getSession().setAttribute("answer", "3");
		} else if (p1.equals("B") && p2.equals("A")) {
			request.getSession().setAttribute("answer", "4");
		}
		Member member = (Member) request.getSession().getAttribute("member");
		Typer typer = new Typer();
		typer.setUsername(member.getUsername());
		typer.setType((String) request.getSession().getAttribute("answer"));
		TypeService ts = new TypeServiceIpmt();
		if (ts.showType(typer.getUsername()) == null) {
			ts.addType(typer);
		} else {
			ts.modType(typer);
		}

		response.sendRedirect("/zzLoveProduct/match/answer.jsp");

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}